package com.example.hp.zyfypt411sy.model;

import com.example.hp.zyfypt411sy.bean.LoginBean;
import com.example.hp.zyfypt411sy.iface.LoginListener;
import com.example.hp.zyfypt411sy.iface.Loginiface;
import com.example.hp.zyfypt411sy.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Mac on 2017/5/9.
 */

public class LoginModel implements Loginiface{
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    //构造函数
    public LoginModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    @Override
    public void getResult(String username,String password , final LoginListener loginListener) {
        //使用Retrofit-2
        LoginService loginService=retrofit.create(LoginService.class);
        Call<LoginBean> login
                =loginService.login(username,password);
        //使用Retrofit-3
        login.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> login, Response<LoginBean> response) {
                if(response.isSuccessful()&&response!=null){
                    loginListener.onResponse( response.body());
                }
                else
                    loginListener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<LoginBean> login, Throwable t) {
                loginListener.onFail(t.toString());
            }
        });
    }
}
